// pages/collect/collect.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        id: 0,
        value: "商品收藏",
        isActive: true
      },
      {
        id: 1,
        value: "品牌收藏",
        isActive: false
      },
      {
        id: 2,
        value: "店铺收藏",
        isActive: false
      },
      {
        id: 3,
        value: "浏览足迹",
        isActive: false
      }
    ],
    index: 0,
    collect: []
  },
  startX: 0,
  endX: 0,
  //点击标题修改
  tabsItemChange(e) {
    const {index} = e.detail;
    //修改源数据
    let {tabs} = this.data;
    tabs.forEach((v, i) => {
      i === index ? v.isActive = true : v.isActive = false;
    });
    this.setData({
      tabs,
      index
    });
  },
  onShow() {
    const collect = wx.getStorageSync('collect') || [];
    this.setData({
      collect
    });
  },
  //滑动切换页面效果
  // 页面开始滑动
  onTouchStart(e) {
    this.startX = e.changedTouches[0].pageX;
  },
  //页面结束滑动
  onTouchEnd(e) {
    //拿取结束值
    this.endX = e.changedTouches[0].pageX;
    if ( Math.abs(this.startX - this.endX) > 160 ) {
      //拿取index赋值
      e.detail.index = (this.startX - this.endX > 0 ?this.data.index + 1 : this.data.index - 1);
      //如果到达最后一页
      e.detail.index = e.detail.index === this.data.tabs.length? 0 : e.detail.index;
      //如果到第一页
      e.detail.index = e.detail.index === -1? this.data.tabs.length - 1 : e.detail.index;
      // console.log(this.data.tabs);
      this.tabsItemChange(e);
    }  
  }
})